package com.rongshu.view.discovery.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.INetView;
import com.rongshu.view.discovery.adapter.EventListAdapter;
import com.rongshu.view.discovery.bean.EventListResponse;
import com.rongshu.view.discovery.presenter.MoreEventPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2019/4/17.
 * description：更多活动
 */
public class MoreEventActivity extends BaseActivity<MoreEventPresenter> implements INetView, OnRefreshLoadmoreListener {
    @BindView(R.id.head_middle)
    TextView headMiddle;
    @BindView(R.id.event_list)
    RecyclerView eventList;
    @BindView(R.id.refresh_view)
    SmartRefreshLayout refresh_view;
    private EventListAdapter adapter;
    private int pageIndex = 1;
    private int pageSize = 10;
    private List<EventListResponse.ResponseBean> beanList;

    @Override
    protected int initLayout() {
        return R.layout.activity_more_event;
    }

    @Override
    protected void initView() {
        headMiddle.setText("活动");
        beanList = new ArrayList<>();
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        eventList.setLayoutManager(manager);
        adapter = new EventListAdapter(this, beanList);
        eventList.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        setPresenter(new MoreEventPresenter(this), this);
        refresh_view.setOnRefreshLoadmoreListener(this);
        refresh_view.autoRefresh();
    }

    @Override
    protected void updateViews(boolean isRefresh) {
    }

    @OnClick(R.id.head_left)
    public void onViewClicked() {
        finish();
    }


    @Override
    public void success(Object object, int tag) {
        EventListResponse response = (EventListResponse) object;
        if (response != null) {
            if (pageIndex == 1) {
                beanList.clear();
            }
            if (refresh_view.isLoading()) {
                refresh_view.finishLoadmore();
            }
            if (refresh_view.isRefreshing()) {
                refresh_view.finishRefresh();
            }
            List<EventListResponse.ResponseBean> list = response.getResponse();
            beanList.addAll(list);
            adapter.notifyDataSetChanged();
            if (list.size() == 0) {
                refresh_view.finishLoadmoreWithNoMoreData();
            }
        }
    }

    @Override
    public void faild(Object object, int tag) {

    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.resetNoMoreData();
        pageIndex = 1;
        presenter.getEvents(pageIndex, pageSize);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        pageIndex++;
        presenter.getEvents(pageIndex, pageSize);
    }
}
